#!/bin/bash
#The plotter is called from any CARBLogger driver
while read line; do
eval $line
done < Config/config

#It depends upon the installation of gnuplot and gnuplotfeed
cleanup()
{
    echo "Caught Signal ... cleaning up."
    if !(rm -f Data/.locked > /dev/null); then
        {
                  exit 1
        }
    fi
	my_interface=`ps -fe | grep interface | grep -v grep | awk '{ print $2 }'`  
    for i in `pgrep ${feed_id} | grep -v ${my_interface}`
    do
    	kill -9 $i 2>/dev/null
    done 
    kill -9 ${feed_id}
    echo "Done cleanup ... quitting."
     exit 0
  }
                                                                                     

########################################################
#	GET THE PATH TO THE CURRENT DRIVER THE PARAMETERS TO PLOT                   #
########################################################
#DIR returns operating directory of the testd script

DIR="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
#Plot dir Provides the CARBLogger Relative Directory
#PLOT_DIR=`echo ${DIR} | sed -s 's/\/home\/aqdms\/CARBLog//g'
#echo $PLOT_DIR
#########################################################
#	CHECK THE EXISTANCE OF NEEDED RESOURCES				#
#	MJV													#
#########################################################
if !( which feedgnuplot ); then
	{
	echo "No feedgnuplot has been found here"
	echo "exiting"
	exit 1
	}
elif !(which gnuplot); then
	{
 	echo "No gnuplot has been found here"
	echo "exiting"
	exit 1
	}
elif !( which pgrep ); then
	{
	echo "No pgrep...stopping"
	exit 1	
	}
fi
####################################################################################
#   SYNCHRONIZE 60 Sec PLOTTING CYCLE WITH FRONT DISPLAY ~ 5 sec after the top of the minute
#####################################################################################
#while [ `date "+%S"` != 25 ]
#do
#    sleep .1
#done

######################################################################
#	GRAB ALL OF THE DISPLAY PARAMETERS AND PLOT THEM
######################################################################
#TRAP CTRL+C
trap cleanup 1 2 3 6 15
catchup=0
while [ : ]; do
{
 if [ $catchup -eq 0 ]; then
    {
     hour=`date '+%H'`
     minute=`date '+%M'`
     for h in `seq -w 0 $hour`
           do
               for m in `seq -w 0 59`
               do
               	(grep "_${h}:${m}" | grep "TLE_so2" | sed 's/--[A-Z]*--//' | sed 's/_/ /g' | sed 's/:::/ /g' | sed 's/so2=//g' | awk '{ print $3 " " $5}')<Data/${saroad}-`date '+%Y'`-`date '+%d'`-`date '+%m'`
     		   done          
		   done
		catchup="1"
    }
 fi	
	
	
	
	
	data=$(tail -n 1000 Data/${saroad}-`date '+%Y'`-`date '+%d'`-`date '+%m'`| grep `date '+_%H:%M'` | grep 'TLE_so2=' )  
	for i in TLE_so2=; 
	do
			if [ `date '+%H:%M'` == "01:00" ]; then
			{
				echo clear
				sleep 60
			}
			else
			{
				echo ${data} | sed 's/--[A-Z]*--//' | sed 's/:::/ /g' | grep -o "${i}[-,0-9].[0-9,.]*" | sed -s 's/'${i}'//g' | awk '{ print "'`date "+%H:%M" `' "$0+0 }' 
				sleep 60
			}
			fi
	done

}
done 2>/dev/null | feedgnuplot --stream --lines --domain --xlabel 'Time [HH:MM]' --ylabel 'PPB' --timefmt '%H:%M' --extracmds 'set format x "%H:%M"; set terminal x11 noraise size 600,400 position 700,550' --points --curvestyle 0 'linewidth 2 linecolor rgb "purple"' --legend 0 "SO2" --title "Sulfur Dioxide"
